package com.hyd.action;

import java.util.List;
import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import com.hyd.daoEx.model.AgeParamEx;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.hyd.dao.model.AgeParam;
import com.hyd.dao.model.AgeParamExample;
import com.hyd.service.AgeParamService;

/**
@author sun
*/
@RequestMapping("ageParam")
@Controller
@LoginFilter
public class AgeParamController extends CoreController{

    @Autowired
    private AgeParamService ageParamService;

    @RequestMapping("/list")
	@ResponseBody
    public String list(Integer page, Integer rows){
    	PaginationSupport.byPage(page, rows);
    	AgeParamExample example = new AgeParamExample();

    	List<AgeParam> list = ageParamService.selectByExample(example);
      	return page(list);
    }

    @RequestMapping("/save")
	@ResponseBody
    public String save(AgeParam ageParam){
    	if (ageParam.getId() == null){
    		//查询是否存在
			AgeParamExample example = new AgeParamExample();
			example.createCriteria().andChemialTypeEqualTo(ageParam.getChemialType());
			List<AgeParam> list = ageParamService.selectByExample(example);
			if(list.size() == 0){
				ageParamService.insert(ageParam);
			}else{
				ageParam.setId(list.get(0).getId());
				ageParamService.updateByPrimaryKeySelective(ageParam);
			}
    	}else{
    		ageParamService.updateByPrimaryKeySelective(ageParam);
    	}
       	return ok();
    }

    @RequestMapping("/findById")
	@ResponseBody
    public String find(Integer id){
    	AgeParam ageParam = ageParamService.selectByPrimaryKey(id);
       	return ok(ageParam);
    }

    @RequestMapping("/del")
	@ResponseBody
    public String del(String id){
    	String[] ids = id.split(",");
    	for (String str : ids){
    		ageParamService.deleteByPrimaryKey(Integer.parseInt(str));
    	}
       	return ok();
    }


	@RequestMapping("/getParamList")
	@ResponseBody
	public String getParamList(Integer chemicalType){
    	AgeParamExample example = new AgeParamExample();
    	example.createCriteria().andChemialTypeEqualTo(chemicalType);
    	example.setOrderByClause("age asc");
    	List<AgeParam> list = ageParamService.selectByExampleWithBLOBs(example);
    	return ok(list);
	}



}